package cn.always.xiajia.admin.sys.service.impl;

import javax.annotation.Resource;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import cn.always.xiajia.admin.sys.entity.SysUser;
import cn.always.xiajia.admin.sys.mapper.SysUserMapper;
import cn.always.xiajia.admin.sys.service.SysUserService;
import cn.always.xiajia.framework.common.enums.UserTypeEnum;
import cn.always.xiajia.framework.mybatis.core.service.impl.BaseServiceImplX;

@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImplX<SysUserMapper, SysUser> implements SysUserService {

	@Resource
	protected PasswordEncoder passwordEncoder;

	@Resource
	protected SysUserMapper sysUserMapper;

	/**
	 * 判断是否管理员
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public boolean isSuperAdmin(Long userId) {
		if (userId == null) {
			return false;
		}
		SysUser sysUser = sysUserMapper.selectById(userId);
		if (sysUser == null) {
			return false;
		}
		if (UserTypeEnum.SUPERADMIN.getValue().toString().equals(sysUser.getUserType())) {
			return true;
		}
		return false;
	}

	/**
	 * 判断密码是否匹配
	 *
	 * @param rawPassword 未加密的密码
	 * @param encodedPassword 加密后的密码
	 * @return 是否匹配
	 */
	@Override
	public boolean isPasswordMatch(String rawPassword, String encodedPassword) {
		return passwordEncoder.matches(rawPassword, encodedPassword);
	}

	/**
	 * 对密码进行加密
	 *
	 * @param password 密码
	 * @return 加密后的密码
	 */
	protected String encodePassword(String password) {
		return passwordEncoder.encode(password);
	}

	/**
	 * 通过用户 ID 查询用户
	 *
	 * @param userId 用户ID
	 * @return 用户对象信息
	 */
	@Override
	public SysUser getUser(Long userId) {
		return sysUserMapper.selectById(userId);
	}
}
